<template>
	<view class="container">
		<u-navbar :is-back="false" title="" :background="background">
			<view class="slot-wrap">首页</view>
<!-- 			<view v-if="userInfo===''" class="slot-right" @click="toLogin">登录</view>
			<view v-if="userInfo!==''">
				<image class="tip-imgbo" src="/static/macroMes.png"></image>
			</view> -->
		</u-navbar>
		<view>
			<home-carousel-map></home-carousel-map>
<!-- 			<home-subhead title="信息查询"></home-subhead>
			<home-middle-navigation :numberOfRows="4" :navList="navList"></home-middle-navigation> -->
			<home-subhead title="业务办理" color="rgba(0, 191, 149, 1)"></home-subhead>
			<home-bottom-navigation ref="bottomNavigation"></home-bottom-navigation>
			<home-bottom-switch ref="bottomSwitch"></home-bottom-switch>
		</view>
		<uni-popup ref="popup" background-color="rgba(0,0,0,0)" type="center" :mask-click="false">
			<view style="height: 100%;width: 100%;">
				<image mode="aspectFit" :src="advImg"></image>
				<view style="text-align:right;">
					<image @click="closePop"  style="width: 55rpx;height: 55rpx;" src="@/static/close.png"/>
				</view>
				<!-- <view class="divX" id="img_zindex_div_1"> -->
				<!-- </view> -->
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		ONLINE_SERVER_ADDRESS_BASE,
		syncGetOrder,
		syncPriceInfo,
		myUploadFile,
	} from "../../util/network.js"
	export default {
		data() {
			return {
				userInfo:[],
				current: 0,
				background : {
					backgroundColor : '#FFFFFF'
				},
				token:'',
				homeImgArray:[],
				advImg:'',
			}
		},
		onLoad: async function() {
			this.userInfo = uni.getStorageSync('userinfo')
			this.token = uni.getStorageSync('token')
			if(uni.getStorageSync('first')){
			  this.getAdvertisement()
			}else{
			  // this.$nextTick(()=>{
			  //   this.$refs.agreement.show(true)
			  // })
			}
			console.log('ceshi',this.userInfo)
			// authRouterGuard(this).then(async (res) => {
			// 	this.userInfo = uniGetStorageSync('userinfo')
			// 	console.log('ceshi',this.userInfo)
			// 	// this.getBanners()
			// 	// this.address = await this.initLocation()
			// })
			
		},
		onUnload() {
			this.stopSetInterval()
		},
		onShow() {
			this.token = uni.getStorageSync('token')
			this.createSetInterval()
		},
		onReady() {
			this.$refs.bottomSwitch.reFresh()
		},
		methods: {
			toLogin(){
				uni.navigateTo({
					url:'/pages/login/login'
				})
			},
			closePop(){
				this.$refs.popup.close()
			},
			getAdvertisement(){
				let _this = this
				uni.request({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/banner/list`,
					data:{ 
							type:1,
					},
					method:'GET',
					success: function (res) {
						console.log('启动页banner',res)
						if(res.data.code == 200){
							let tempId = res.data.data.bannerList[0].pictureId.split(',')[0]
							let b = _this.loadImg(tempId)
							b.then((v)=>{
								_this.advImg = v
								_this.$refs.popup.open()
							})
						}
					}
				})
			},
			async loadImg(fileId){
				// let _this = this
				const res = await uni.downloadFile({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/file/download/` + fileId,
					header: {
						'Authorization': this.token,
					},
				})
				return new Promise((resolve, reject) => {
				        resolve(res[1].tempFilePath)
				})
			},
			getPending(){
				let _this = this
				uni.request({
					url:`${ONLINE_SERVER_ADDRESS_BASE}/message/get-pending`,
					header: {
						'Authorization': this.token,
					},
					data:{ 
							goodsReleaseStatus:'2',         //物品放行
							accessCardStatus:'2',          //门禁卡
							whiteCardStatus:'3,6',           //白卡
							parkingCardStatus:'2',         //停车位
							airConditionStatus:'1',        //空调加时
							conferenceRoomStatus:'1',      //会议室预约
							repairStatus:'3,5',              //维修服务
							decorationStatus:'5,9,13',          //装修服务
							invoiceStatus:'3',             //发票办理
							costServiceStatus:'2',         //收费服务
							goodsBorrowStatus:'1,4',         //物品借用
							knotCommissionStatus:'0',      //中介结佣
							deliverHouseStatus:'2',        //交房
							exchangeRentStatus:'3,5',        //退换租
							renewalStatus:'0,6,9',             //续租
							onlinePayStatus:'0',            //缴费服务
					},
					method:'POST',
					success: function (res) {
						// console.log('待处理',res)
						if(res.data.code == 200){
							_this.$pendingObj.setPendingObj(res.data.data)
							// _this.$root.pendingObj = res.data.data
							_this.$refs.bottomNavigation.reFreshPendingNum()
							_this.$refs.bottomSwitch.reFreshPendingNum()
							let temp = 0
							if(res.data.data.onlinePay > 0){
								temp++
							}
							if(res.data.data.renewal > 0){
								temp++
							}
							if(res.data.data.exchangeRent > 0){
								temp++
							}
							if(res.data.data.deliverHouse > 0){
								temp++
							}
							if(temp > 0){
								uni.setTabBarBadge({
									index: 4,
									text: temp.toString(),
								});
							}else{
								uni.removeTabBarBadge({
									index: 4,
								});
							}
						}
						if(res.data.code != 200){
							let tempObj = { 
								goodsRelease:0,         //物品放行
								accessCard:0,          //门禁卡
								whiteCard:0,           //白卡
								parkingCard:0,         //停车位
								airCondition:0,        //空调加时
								conferenceRoom:0,      //会议室预约
								repair:0,              //维修服务
								decoration:0,          //装修服务
								invoice:0,             //发票办理
								costService:0,         //收费服务
								goodsBorrow:0,         //物品借用
								knotCommission:0,      //中介结佣
								deliverHouse:0,        //交房
								exchangeRent:0,        //退换租
								renewal:0,             //续租
								onlinePay:0,            //缴费服务
								total: 0
							}
							console.log(tempObj)
							_this.$pendingObj.setPendingObj(tempObj)
							_this.$refs.bottomNavigation.reFreshPendingNum()
							_this.$refs.bottomSwitch.reFreshPendingNum()
							uni.removeTabBarBadge({
								index: 4,
							});
						  if (_this.$myTimer.myTimer) {
							clearInterval(_this.$myTimer.myTimer)
							_this.$myTimer.setTimer(null)
						  }
						}
					},
					fail: (res) => {
						let tempObj = { 
							goodsRelease:0,         //物品放行
							accessCard:0,          //门禁卡
							whiteCard:0,           //白卡
							parkingCard:0,         //停车位
							airCondition:0,        //空调加时
							conferenceRoom:0,      //会议室预约
							repair:0,              //维修服务
							decoration:0,          //装修服务
							invoice:0,             //发票办理
							costService:0,         //收费服务
							goodsBorrow:0,         //物品借用
							knotCommission:0,      //中介结佣
							deliverHouse:0,        //交房
							exchangeRent:0,        //退换租
							renewal:0,             //续租
							onlinePay:0,            //缴费服务
							total: 0
						}
						_this.$pendingObj.setPendingObj(tempObj)
						_this.$refs.bottomNavigation.reFreshPendingNum()
						_this.$refs.bottomSwitch.reFreshPendingNum()
						uni.removeTabBarBadge({
							index: 4,
						});
					}
				})
			},

		// 开启轮询  如果存在则先销毁定时器后重新开启
			createSetInterval() {
			  this.stopSetInterval()
			  let _this = this
			  this.$myTimer.setTimer(setInterval(() => {
				_this.getPending()
			  }, 1000))
			},
			// 关闭轮询
			stopSetInterval() {
			  if (this.$myTimer.myTimer) {
				clearInterval(this.$myTimer.myTimer)
				this.$myTimer.setTimer(null)
			  }
			}
		},
	}
</script>

<style scoped lang="scss">
	/* 底部内边距为150rpx跟底部离一小段距离UI效果更好 */
	.container {
		padding-bottom: 0rpx;
		background-color: #FFFFFF;
	}
	.slot-wrap {
		display: flex;
		align-items: center;
		padding-left: 40rpx;
		font-size: 35rpx;
		font-weight: 700;
	}
	.slot-right{
		display: flex;
		align-items: flex-end;
		padding-left: 540rpx;
		font-size: 30rpx;
		color: #0000FF;
	}
	.tip-imgbo {
		flex-direction:flex;
		height: 45rpx;
		width: 45rpx;
		padding-left: 580rpx;
		margin-top: 15rpx;
		font-size: 35rpx;
	}
	.divX
	{
	    z-index:200;
	    -moz-border-radius:20px;
	    -webkit-border-radius:20px;
	    line-height:10px;
	    text-align:center;
	    font-weight:bold;
	    cursor:pointer;
	    font-size:10px;
	    display: none;
	}
</style>
